home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 408 < prev    next >
Internet Message Format  |  1996-08-06  |  4KB

  1. Path: solon.com!not-for-mail
  2. From: tada@athena.mit.edu (Michael J Zehr)
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion.
  5. Date: 16 Feb 1996 00:10:37 -0600
  6. Organization: Massachusetts Institute of Technology
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4g174t$mq7@solutions.solon.com>
  10. References: <4fstj7$2l6@solutions.solon.com> <4fvk8c$eq8@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4fvk8c$eq8@solutions.solon.com> Ari Lukumies <aril@cmt.lpr.mail.carel.fi> writes:
  14. >Rune Huseby wrote:
  15. >: short test(short x1, short x2);
  16. >: int main(void)
  17. >: {
  18. >:   short result;
  19. >:   result = test(1, 2);
  20. >:   return 0;
  21. >: }
  22.  
  23. >The numbers 1 and 2 here are by convention considered by compiler to be ints (not 
  24. >shorts).
  25.  
  26. Yes, but because a prototype is in scope for the function "test" they
  27. are converted to the types of the parameters (as if by assignment), so
  28. the compiler converts 1 and 2 to shorts before passing them.
  29.  
  30. >: short test(short x1, short x2)
  31. >: {
  32. >:   short result;
  33. >:   result = x1 + x2;  /* Warning:  '=' : conversion from 'int '
  34. >:                          to 'short ', possible loss of data */
  35. >:   return result;
  36. >: }
  37. >: 
  38. >: My compiler (Microsoft Visual C++ 4.0), automagically converts
  39. >: my short-parameters to int's, even though all variables involved
  40. >: are short. I know that the standard says that all arguments can
  41. >: be converted to the biggest 'type' of all the arguments, but is
  42. >: it correct that char's and short's always are promoted to int's,
  43. >: without regard to the other arguments in the expression?
  44. >
  45. >Chars are converted to ints, because passing a byte (where char equals one byte in 
  46. >size) is both inefficient and leads to difficulties in the receiving party (for 
  47. >instance, to pass two chars would then pack them into one byte, which the receiver 
  48. >would have to be able to handle).
  49.  
  50.  
  51. The mechanism of passing and how much space the parameters take up on
  52. the stack (if one exists!) is a complete black box as far as the
  53. standard goes.  It can turn them into hex and engrave the parameter
  54. values on papyrus reeds and have the called function run an OCR to get
  55. the values off for all the standard says.  But if the function
  56. parameter is called a short in the function definition and the program
  57. doesn't violate any constraints (the most likely one to break here would
  58. be a new-style function definition called without a function declaration
  59. in scope) then the parameters are shorts in the function body.  This is
  60. true even on PC compilers that might not be conforming because of other
  61. reasons.
  62.  
  63. Now what *really* happens here is described in the appendix of K&R2:
  64. A6.1 Integral Promotion:  A character [...] may be used in an expression
  65. wherever an integer may be used. [...] the value is converted to int;
  66.  
  67. [This article was posted to both comp.std.c and comp.lang.c.moderated.
  68. For the former I ought to be quoting form the standard.  For the latter,
  69. far more people have access to K&R2 to check things than the standard.]
  70.  
  71. So while 
  72.  
  73. x1 and x2 are shorts and remain shorts regardless of how the compiler
  74. sees fit to pass them to the "test" function, in the expression "x1 +
  75. x2" there values are converted to ints.  Thus the type of "x1 + x2" is
  76. an int and it is *this* that is being converted to a short during the
  77. assignment to result.
  78.  
  79. The original poster was mostly right in the last sentence -- the values
  80. of short variables in an expression are convert to int if an int can
  81. hold all possible values, otherwise converted to an unsigned int.  It is
  82. neither the arguments nor the parameters that are being converted.
  83.  
  84. -michael j zehr
  85.